﻿using Business.BorrowManagement;
using Business.BorrowManagement.Dto;
using Business.InvenManagement.Dto;
using Business.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;
using Yitter.IdGenerator;

namespace Business.InvenManagement
{
    public class InvenAppService : BusinessWorkFlowAppService, IInvenAppService
    {
        private const string FormName = "Inven";
        private IRepository<Inven, Guid> _repository;
        private IRepository<InDetail, Guid> _repository1;

        public InvenAppService(
           IRepository<Inven, Guid> repository, IRepository<InDetail, Guid> repository1
           )
        {
            _repository = repository;
            _repository1 = repository1;
        }
        public async Task<InvenDto> Get(Guid id)
        {
            var query = (from b in await _repository.GetQueryableAsync()
                         where b.Id == id
                             // on b.TenantId equals d.BorrowId
                         select new InvenDto
                         {
                             Id = b.Id,
                             ITitle = b.ITitle,
                             INum = b.INum,
                             ITime = b.ITime,
                             IMan = b.IMan,
                             IPro = b.IPro,
                             IDesc = b.IDesc,
                             IBase = b.IBase,
                             IProgram = b.IProgram,


                             createOrUpdateInvenDto = (from d in _repository1.GetQueryableAsync().Result
                                                       where b.Id == d.InvenId
                                                       select new CreateOrUpdateInvenDto
                                                       {

                                                           ProductName = d.ProductName,
                                                           ProductNum = d.ProductNum,
                                                           SkuNum = d.SkuNum,
                                                           PUnit = d.PUnit,
                                                           FaceNum = d.FaceNum,
                                                           ReallyNum = d.ReallyNum,
                                                           ShopNum = d.ShopNum,
                                                           ProPrice = d.ProPrice,
                                                           ShopPrice = d.ShopPrice,
                                                           BaseName = d.BaseName,
                                                           Desc = d.Desc

                                                       }
                             ).ToList()




                         }
                      ).ToList();



            //var data = await _repository.GetAsync(id);
            //var dto = ObjectMapper.Map<BorrowM, BorrowDto>(data);
            return query.ToList()[0];
        }

        public async Task<PagedResultDto<InvenDto>> GetAll(GetInvenInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.BTitle.Contains(input.Filter));

            var query = (from b in await _repository.GetQueryableAsync()

                             // on b.TenantId equals d.BorrowId
                         select new InvenDto
                         {
                             Id = b.Id,
                             ITitle = b.ITitle,
                             INum = b.INum,
                             ITime = b.ITime,
                             IMan = b.IMan,
                             IPro = b.IPro,
                             IDesc = b.IDesc,
                             IBase = b.IBase,
                             IProgram = b.IProgram,


                             createOrUpdateInvenDto = (from d in _repository1.GetQueryableAsync().Result
                                              where b.Id == d.InvenId
                                              select new CreateOrUpdateInvenDto { 

                                                  ProductName = d.ProductName,
                                                  ProductNum = d.ProductNum,
                                                  SkuNum = d.SkuNum,
                                                  PUnit = d.PUnit,
                                                  FaceNum = d.FaceNum,
                                                  ReallyNum = d.ReallyNum,
                                                  ShopNum = d.ShopNum,
                                                  ProPrice = d.ProPrice,
                                                  ShopPrice = d.ShopPrice,
                                                  BaseName = d.BaseName,
                                                  Desc = d.Desc

                                              }
                             ).ToList()




                         }
                       ).ToList();



            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();

            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<InvenDto>(totalCount, query);
        }

        public async Task<InvenDto> CreateOrUpdate(InvenDto input)
        {
            Inven result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                //雪花Id
                input.INum = YitIdHelper.NextId().ToString();
                Inven borrow = new Inven()
                {
                    ITitle = input.ITitle,
                    INum = input.INum,
                    ITime = input.ITime,
                    IMan = input.IMan,
                    IPro = input.IPro,
                    IDesc = input.IDesc,
                    IBase=input.IBase,
                    IProgram = input.IProgram,
                  

                };
                foreach (var item in input.createOrUpdateInvenDto)
                {
                    InDetail detail = new InDetail()
                    {
                        InvenId = input.Id,
                        ProductName = item.ProductName,
                        ProductNum = item.ProductNum,
                        SkuNum = item.SkuNum,
                        PUnit = item.PUnit,
                        FaceNum = item.FaceNum,
                        ReallyNum = item.ReallyNum,
                        ShopNum = item.ShopNum,
                        ProPrice = item.ProPrice,
                        ShopPrice = item.ShopPrice,
                        BaseName=item.BaseName,
                        Desc = item.Desc
                    };
                    await _repository1.InsertAsync(detail);

                };
                result = await _repository.InsertAsync(ObjectMapper.Map<InvenDto, Inven>(input));

                //创建工作流
                // await WorkFlow.CreateWorkFlow(FormName, result);
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
                //检查流程状态
                //await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }
            return ObjectMapper.Map<Inven, InvenDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }


    }
}
